package com.example.dayday.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.example.dayday.dto.ExcelTeacherDTO;
import com.example.dayday.dto.TeacherDTO;
import com.example.dayday.listener.ExcelDTOListener;
import com.example.dayday.pojo.Student;
import com.example.dayday.pojo.Teacher;
import com.example.dayday.mapper.TeacherMapper;
import com.example.dayday.service.TeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Helen
 * @since 2022-09-07
 */
@Service
@Slf4j
@Transactional(rollbackFor = {Exception.class})
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;

    //自己修改密码
    public boolean updatePassword(String oldPassword, String newPassword, Long studentId) {
        LambdaUpdateWrapper<Teacher> wq = new LambdaUpdateWrapper<>();
        wq.eq(Teacher::getTeacherId,studentId);
        wq.eq(Teacher::getPassword,oldPassword);
        wq.set(Teacher::getPassword,newPassword);
        return update(wq);
    }

    //管理员修改老师密码
    @Override
    public boolean updatePassword(String newPassword, Long studentId) {
        LambdaUpdateWrapper<Teacher> wq = new LambdaUpdateWrapper<>();
        wq.eq(Teacher::getTeacherId,studentId);
        wq.set(Teacher::getPassword,newPassword);
        return update(wq);
    }

    @Override
    public void importData(InputStream inputStream) {
        EasyExcel.read(inputStream, ExcelTeacherDTO.class, new ExcelDTOListener(teacherMapper)).sheet("Sheet1").doRead();
        log.info("importData finished");
    }
}
